<template>
    <div>
        <h2>更新会员</h2>
        <table class="table">
        <thead>
            <tr>
                <td>name</td>
                <td><input type="text" v-model="obj.name"></td>
            </tr>
            <tr>
                <td>password</td>
                <td><input type="password" v-model="obj.password"></td>
            </tr>
            <tr>
                <td>会员等级</td>
                <td>
                    <select v-model="obj.levelId">
                        <option value="0">请选择会员等级</option>
                        <option v-for="a in data" :value="a.id">{{a.title}}</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td>状态</td>
                <td><input type="radio" value="1" v-model="obj.starts" name="starts" >正常
                    <input type="radio" value="2" v-model="obj.starts" name="starts" >冻结
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <input type="button" value="更新" @click="Upd">
                </td>
            </tr>
        </thead>
       </table>
        <a href="/">返回会员列表</a>
    </div>
</template>

<script setup lang="ts">
import axios from 'axios';
import {ref,onMounted} from 'vue'
import { useRoute,useRouter } from 'vue-router';
const route=useRoute()
const router=useRouter()

onMounted(()=>{
    BindLevel()
    FindMember()
})

//动态读取下拉框数据
const data=ref({})

const BindLevel=()=>{
    axios.get("/api/MemberLevel/GetAllMemberLevel",{}).then(res=>{
      data.value=res.data
    })
}
//反填
const FindMember=()=>{
    //取 地址栏 编号
    var uid=route.query.id
    axios.get("/api/Member/FindMember",{params:{id:uid}}).then(res=>{
        obj.value.id=res.data.id;
        obj.value.name=res.data.name;
        obj.value.password=res.data.password;
        obj.value.levelId=res.data.levelId;
        obj.value.starts=res.data.starts;
    })
}
const obj=ref({
    id:0,
    name:'',
    password:'',
    levelId:0,
    starts:1
})

//修改
const Upd=()=>{
    console.log(obj.value)
    
    axios.put("/api/Member/UpdateMember",obj.value).then(res=>{
     if(res.data>0)
     {
        alert('更新成功!')
        router.push('/')
     }
     else
     {
        alert('更新失败')
     }
    })
}

</script>

<style scoped>

</style>